<template>
  <div class="my-container">
    <!-- 已登录 -->
    <div class="userInfo" v-if="user">
      <!-- 基本信息 -->
      <div class="base">
        <div class="left">
          <van-image fit="cover" :src="userInfo.photo" class="avatar" round />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button type="default" size="mini" to="/user/profile" round
            >编辑资料</van-button
          >
        </div>
      </div>
      <!-- 粉丝、关注 -->
      <div class="data">
        <div class="data-item">
          <span>{{ userInfo.art_count }}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{ userInfo.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{ userInfo.fans_count }}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{ userInfo.like_count }}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- 未登录头部 -->
    <div class="users" v-else>
      <div class="header">
        <img
          class="mobile-img"
          src="~@/assets/phone.png"
          @click="$router.push('/login')"
        />
        <span class="login">登录/注册</span>
      </div>
      <!-- <div class="grid-nav">
      <van-cell title="消息通知" is-link url="" />
      <van-cell title="实名认证" is-link url="" />
      <van-cell title="用户反馈" is-link url="" />
      <van-cell title="小智同学" is-link url="" />
      <van-cell title="系统设置" is-link url="" />
    </div> -->
    </div>

    <!-- 宫格 -->
    <van-grid class="nav-grid" :column-num="2" clickable>
      <van-grid-item text="收藏">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span class="text" slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item text="文字">
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span class="text" slot="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 单元格导航 -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell
      v-if="user"
      class="logout-cell"
      title="退出登录"
      center
      @click="affirmFn"
      clickable
    />
  </div>
</template>

<script>
import { mapState } from "vuex";

import { getUserInfo } from "@/api/user";
export default {
  name: "my",
  components: {},
  props: {},
  data() {
    return {
      userInfo: "",
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  watch: {},
  created() {
    // 初始化的时候，如果用户登录了，我才请求获取当前登录用户的信息
    if (this.user) {
      this.loadUser();
    }
  },
  mounted() {},
  methods: {
    affirmFn() {
      // 退出提示
      // 在组件中需要使用 this.$dialog 来调用弹框组件
      this.$dialog
        .confirm({
          title: "确认退出吗？",
        })
        .then(() => {
          // 确认退出：清除登录状态（容器中的 user + 本地存储中的 user）
          this.$store.commit("setUser", null);
        })
        .catch(() => {
          console.log("取消退出操作");
        });
    },

    //对获取用户信息的接口进行二次封装；
    async loadUser() {
      try {
        const { data } = await getUserInfo();
        console.log(data.data);
        this.userInfo = data.data;
      } catch (err) {
        console.log(err);
        this.$toast("获取数据失败");
      }
    },
  },
};
</script>

<style scoped lang="less">
.my-container {
  .users {
    .header {
      height: 361px;
      background: url("~@/assets/beijing.png") no-repeat;
      background-size: cover;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      .login {
        font-weight: 700;
        color: #fff;
      }
      .mobile-img {
        width: 132px;
        height: 132px;
        margin-bottom: 20px;
      }
    }
    .header {
      width: 750px;
      height: 401px;
      background: url("~@/assets/beijing01.jpg");
      background-size: cover;
    }
  }
}

.userInfo {
  background: url("~@/assets/beijing01.jpg");
  background-size: cover;

  .base {
    height: 231px;
    padding: 76px 32px 23px;
    // background: #ccc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    .left {
      display: flex;
      align-items: center;
      .avatar {
        width: 132px;
        height: 132px;
        border: 2px solid #fff;
      }
      .name {
        font-size: 29px;
        margin-left: 22px;
        color: #001938;
      }
    }
  }
}
.logout-cell {
  height: 100px;
  text-align: center;
  color: #eb5253;
  margin: 10px 0;
  font-size: 30px;
}
.data {
  display: flex;
  justify-content: space-around;
  .data-item {
    height: 130px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: #fff;
    span:nth-child(1) {
      font-size: 36px;
      margin-bottom: 15px;
    }
    span:nth-child(2) {
      font-size: 23px;
    }
  }
}
.nav-grid {
  background: #fff;
  .toutiao {
    font-size: 45px;
  }
  .toutiao-shoucang {
    color: #eb5253;
  }
  .toutiao-lishi {
    color: #ff9d1d;
  }
  .text {
    color: #000;
    margin-top: 8px;
    font-size: 28px;
  }
}
</style>